package com.kangaroo.myhome.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.kangaroo.myhome.mapper.TInspTaskDetailMapper;
import com.kangaroo.myhome.mapper.TInspTaskMapper;
import com.kangaroo.myhome.model.CommonSearch;
import com.kangaroo.myhome.model.TInspTask;
import com.kangaroo.myhome.model.TInspTaskDetail;
import com.kangaroo.myhome.model.TInspTaskDetailExample;
import com.kangaroo.myhome.vo.ResponseModel;

import tk.mybatis.mapper.util.StringUtil;

@Service
public class TInspTaskService {

    @Autowired
    private TInspTaskMapper tInspTaskMapper;

    @Autowired
    private TInspTaskDetailMapper tInspTaskDetailMapper;

    /**
     * @param page
     * @param rows
     * @return
     */
    public ResponseModel findAll(String name, int page, int rows) {
        ResponseModel model = new ResponseModel();

        CommonSearch user = new CommonSearch();
        user.setString5(name);
        user.setPagination(page, rows);

        List<TInspTask> users = tInspTaskMapper.selectByPagination(user);
        long count = tInspTaskMapper.selectByPaginationCount(user);
        model.setCode(200);
        model.setMessage("数据获取成功");
        model.setDataDataGrid(users, count);
        return model;
    }

    /**
     * 功能描述: 
     *
     * @param taskId:
     * @author: kangaroo
     * @date: 2020/11/22 23:19
     * @return:
     */
    public ResponseModel findAllDetail(String taskId, int page, int rows) {
        ResponseModel model = new ResponseModel();

        TInspTaskDetailExample example = new TInspTaskDetailExample();
        example.createCriteria().andTaskIdEqualTo(taskId);
        example.setOrderByClause("opt_time asc");
        List<TInspTaskDetail> details = tInspTaskDetailMapper.selectByExample(example);
        long count = tInspTaskDetailMapper.countByExample(example);

        model.setCode(200);
        model.setMessage("数据获取成功");
        model.setDataDataGrid(details, count);
        return model;
    }





    /**
     * @param tInspTask
     * @return TInspProject tInspProject
     */
    public ResponseModel edit(TInspTask tInspTask) {

        ResponseModel model = new ResponseModel();
        if(tInspTask == null || StringUtil.isEmpty(tInspTask.getName() )){
            model.setCode(201);
            model.setMessage("参数异常");
            return model;
        }
        tInspTask.setId(UUID.randomUUID().toString());
        int resultCount = tInspTaskMapper.insertSelective(tInspTask);
        if(resultCount>0){
            model.setCode(200);
            model.setMessage("操作成功");
        }else{
            model.setCode(201);
            model.setMessage("保存数据库异常");
        }
        return model;
    }


    /**
     * 功能描述: 
     *
     * @param tInspTaskDetail:
     * @author: kangaroo
     * @date: 2020/11/22 23:26
     * @return:
     */
    public ResponseModel editDetail(TInspTaskDetail tInspTaskDetail) {

        ResponseModel model = new ResponseModel();
        if(tInspTaskDetail == null || StringUtil.isEmpty(tInspTaskDetail.getTaskId() )){
            model.setCode(201);
            model.setMessage("参数异常");
            return model;
        }

        tInspTaskDetail.setId(UUID.randomUUID().toString());
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        tInspTaskDetail.setOptTime(format.format(new Date()));
        int resultCount = tInspTaskDetailMapper.insertSelective(tInspTaskDetail);
        if(resultCount>0){
            model.setCode(200);
            model.setMessage("操作成功");
        }else{
            model.setCode(201);
            model.setMessage("保存数据库异常");
        }
        return model;
    }


    public void delete(TInspTask tInspTask) {
        String ids = tInspTask.getId();
        String[] idArr = ids.split(",");
        for(String id : idArr){
            tInspTaskMapper.deleteByPrimaryKey(id);

        }
    }

    public void deleteDetail(TInspTaskDetail tInspTaskDetail) {
        String ids = tInspTaskDetail.getId();
        String[] idArr = ids.split(",");
        for(String id : idArr){
            tInspTaskDetailMapper.deleteByPrimaryKey(id);

        }
    }

}
